#ifndef _MESSAGES_H_
#define _MESSAGES_H_

#include <defs.h>

// Request types
#define DELAY_REQUEST         0   // Delay()
#define TIME_REQUEST          1   // Time()
#define DELAY_UNTIL_REQUEST   2   // DelayUntil()

// Clock requests (e.g. from system requests)
typedef struct __clock_req {
  int type;
  int ticks;
} clock_req;

/*
// Requests to the engine server
typedef struct __engine_req {
  int type;
  int speed;
  sensor last;
} engine_req;
*/

// Request types
#define WHO_IS_REQUEST        0
#define REGISTER_AS_REQUEST   1
#define DEPENDS_ON_REQUEST    2

// Reply errors
#define ERROR_TASK_NOT_FOUND  -1
#define ERROR_TABLE_FULL      -2

typedef struct __name_req {
  int type;
  char name[MAX_NAME_LEN];
  int tid;
} name_req;

#define STALLED   0
#define TXREADY   1

#define MSG_RX_HASC   0
#define MSG_RX_GETC   1
#define MSG_RX_DATA   2

typedef struct __rx_req {
  int type;
  int len;
  char ch[16];
} rx_req;

typedef struct __terminal_rx_req {
  int type;
  int len;
  char ch[16];
} terminal_rx_req;

typedef struct __rx_reply {
  int ch;
} rx_reply;

typedef struct __tx_req {
  char *str;
  int len;
} tx_req;

#define REQ_BUFFER  0
#define REQ_STATUS  1

#define TX_INT  0
#define MS_INT  1

typedef struct __train_tx_req {
  int state;
  char ch;
} train_tx_req;

typedef struct __train_tx_reply {
  int len;
  int buf[16];
} train_tx_reply;


#define MSG_GET_SWITCH_STATE      0
#define MSG_UPDATE_SWITCH_STATE   1

// switch request
typedef struct __switch_req {
  int type;
  int switch_num;
  int switch_dir;
} switch_req;

// switch reply
typedef struct __switch_reply {
  int ret;
} switch_reply;


#define RESERVE_TRACK_REQUEST   0
#define FREE_TRACK_REQUEST      1
#define RESERVE_SWITCH_REQUEST   2
#define FREE_SWITCH_REQUEST      3

// reservation request struct
typedef struct __reserve_req {
  int type;
  int v1;
  int v2;
} reserve_req;

// reservation reply struct
typedef struct __reserve_reply {
  int result;
} reserve_reply;

// route finder request struct
typedef struct __route_req {
  int a;
  int b;
  int *path;
} route_req;

// route finder reply struct
typedef struct __route_reply {
  int result;
} route_reply;

typedef struct __engine_init {
  int train;
  int starting_landmark;
  int next_landmark;
  int status_line;
} engine_init;

typedef struct __engine_req {
  int type;
  int speed;
  int landmark;
  char last_sensor[5];
} engine_req;

typedef struct __engine_reply {
  int result;
  int result2;
} engine_reply;

typedef struct __path_req {
  int type;
  int source;
  int target;
  int train;
} path_req;

typedef struct __tron_msg {
  int train;
  int pos;
  int next;
  int opponent;
} tron_msg;

#endif
